/**
 * Created by wangjian on 2017/10/20.
 * 富文本编辑框
 * mhtml：编辑框内容，双向绑定
 */
angular.module('app').component('mWysiwyg', {
    templateUrl: getTemplateUrl('matrix/component/tpl/mWysiwyg.html'),
    bindings: {
        label: '@',
        mhtml: '=',
        isDisbaled: '=',
        mid: '@',
        isEdit: '<',
        changeData: '&',
        validate: '@',
        iconType: '@',
        plugin  : '@',
        changeHeight : '@'
    },
    controller: function ($scope, $element, $attrs, $compile, $timeout) {
        var ctrl = this;
        $scope.$on("to-wsying", function(event, d) {
            if(d != null) {
                ctrl.mhtml = d.prototype;
            } else {
                ctrl.mhtml = "";                
            }
            
        });

        ctrl.$onChanges = function(data) {
            if(data.isEdit) {
                if(data.isEdit.currentValue == false) {
                    $('#' + ctrl.mid).froalaEditor('edit.on');
                }
            }
        }

        $scope.$on("getWsyingFun", function(event, data) {
            getWsyingFun();
        });
        
        function getWsyingFun() {};

        if (ctrl.mhtml == undefined || ctrl.mhtml == 'undefined') {
            ctrl.mhtml = '';
        }
        if (ctrl.mid == undefined || ctrl.mid == 'undefined') {
            ctrl.mid = 'mWysiwyg';
        }

        var toolbarButtons = ['underline', 'strikeThrough', 'subscript', 'superscript', '|', 'fontFamily', 'fontSize', 'color', 'inlineStyle', 'paragraphStyle', '|', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'outdent', 'indent', 'quote', '-', 'insertLink', 'insertImage', 'insertTable', '|', 'emoticons', 'specialCharacters', 'insertHR', 'selectAll', 'clearFormatting', '|', 'print', 'help', 'html', '|', 'undo', 'redo'];
        if (ctrl.iconType != undefined && ctrl.iconType == 'text') {
            if(ctrl.plugin != undefined) {
                toolbarButtons = [ctrl.plugin];
            } else {
                toolbarButtons = ['underline', 'strikeThrough', 'subscript', 'superscript', '|', 'fontFamily', 'fontSize', 'color', 'inlineStyle', 'paragraphStyle', '|', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'outdent', 'indent', 'quote', '-', 'insertLink', 'insertImage', 'insertTable', '|', 'emoticons', 'specialCharacters', 'insertHR', 'selectAll', 'clearFormatting', '|', 'print', 'help', 'html', '|', 'undo', 'redo'];
            }
        } else if (ctrl.iconType != undefined && ctrl.iconType == 'img') {
            toolbarButtons = ['insertImage'];
        }

        if(ctrl.plugin != undefined) {
            ctrl.froalaOptions = {
                toolbarButtons: toolbarButtons,
                pluginsEnabled: null,
                // heightMin: 300,
                quickInsertButtons: [],
                imageDefaultWidth: 600,
                imageDefaultAlign: 'left',
            }
        } else {
            ctrl.froalaOptions = {
                toolbarButtons: toolbarButtons,
                pluginsEnabled: null,
                quickInsertButtons: [],
                imageDefaultWidth: 600,
                imageDefaultAlign: 'left',
            }
        }

        $timeout(function () {
            if (ctrl.isEdit == 'true' || ctrl.isEdit == true) {
                $('#' + ctrl.mid).froalaEditor('edit.off');
            } else {
                if(ctrl.isFile == "true") {
                    $('#' + ctrl.mid).froalaEditor('edit.off');
                } else {
                    $('#' + ctrl.mid).froalaEditor('edit.on');
                }
                $('#' + ctrl.mid).on('froalaEditor.blur', function (e, editor) {
                    ctrl.changeData({value: e.currentTarget.value});
                })
            }
            $('#' + ctrl.mid).on('froalaEditor.image.beforeUpload', function (e, editor, files) {
                if (files.length) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var result = e.target.result;
                        editor.image.insert(result, null, null, editor.image.get());
                    };
                    reader.readAsDataURL(files[0]);
                }
                return false;
            })
        }, 500);
    }
});
/**
 * switch
 * status:默认状态  true false
 * mkey:要显示data里的哪个字段
 * mclickFun:点击switch后调用函数
 */
angular.module('app').component('mSwitch', {
    templateUrl: getTemplateUrl('matrix/component/tpl/mSwitch.html'),
    bindings: {
        label: '=',
        status: '=',
        trueValue: '=',
        falseValue: '='
    },
    controller: function ($scope, $element, $attrs, $compile) {
        console.log(this)
        var ctrl = this;

        if (typeof(ctrl.falseValue) == "undefined") {
            ctrl.trueValue = 0;
        }
        if (typeof(ctrl.falseValue) == "undefined") {
            ctrl.falseValue = 1;
        }

    }
});
